package com.css.fxfzxczh.modules.evaluationSummary.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.actionTrack.entity.ActionTrackEntity;
import com.css.fxfzxczh.modules.evaluationSummary.entity.*;
import com.css.fxfzxczh.modules.evaluationSummary.service.EnterpriseDirectEconomyService;
import com.css.fxfzxczh.modules.evaluationSummary.vo.ParamsVo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.CasualtyInvestigation;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 模块:
 *      室内外财产损失评估
 *      生命线系统直接损失评估
 *      企事业直接经济损失评估
 *      人员伤亡分析
 */
@RestController
@RequestMapping("/evaluationSummary")
@Slf4j
public class EvaluationSummaryController {

    @Autowired
    EnterpriseDirectEconomyService enterpriseDirectEconomyService;

    /**
     * 企事业直接经济损失评估
     * 计算前先校验
     * @param xsemId  当前地震事件id
     * @return
     */
    @GetMapping("/checkData")
    public RestResponse checkData(String xsemId){
        RestResponse restResponse=null;
        try{
            String message = enterpriseDirectEconomyService.checkData(xsemId);
            restResponse = RestResponse.succeed(message);
            restResponse.setMessage("校验成功");
        }catch (Exception e){
            String errormessage="校验失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 企事业直接经济损失评估
     * 通过房屋直接经济损失估算
     *
     * @return
     */
    @PostMapping("/getEvaluationSubArea")
    public RestResponse getEvaluationSubArea(@RequestBody ParamsVo paramsVo){
        RestResponse restResponse=null;
        try{
            enterpriseDirectEconomyService.getEvaluationSubArea(paramsVo);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 企事业直接经济损失评估
     * 页面加载时查询
     * @param xsemId   当前地震事件id
     * @return
     */
    @GetMapping("/getData")
    public RestResponse getData(String xsemId){
        RestResponse restResponse=null;
        try{
            Map<String, Object> map = enterpriseDirectEconomyService.getData(xsemId);
            restResponse = RestResponse.succeed(map);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 室内外财产损失评估   计算前校验
     * @param xsemId
     * @return
     */
    @GetMapping("/indoorOutdoorCheckData")
    public RestResponse indoorOutdoorCheckData(String xsemId){
        RestResponse restResponse=null;
        try{
            String message = enterpriseDirectEconomyService.indoorOutdoorCheckData(xsemId);
            restResponse = RestResponse.succeed(message);
            restResponse.setMessage("校验成功");
        }catch (Exception e){
            String errormessage="校验失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }



    /**
     * 室内外财产损失评估
     * 通过房屋直接经济损失估算
     *
     * @return
     */
    @PostMapping("/getIndoorOutdoorPropertyLosses")
    public RestResponse getIndoorOutdoorPropertyLosses(@RequestBody ParamsVo paramsVo){
        RestResponse restResponse=null;
        try{
            enterpriseDirectEconomyService.getIndoorOutdoorPropertyLosses(paramsVo);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 室内外财产损失评估
     * 结果列表查询
     * @param xsemId   当前地震事件id
     * @return
     */
    @GetMapping("/getIndoorOutdoorPropertyLossesData")
    public RestResponse getIndoorOutdoorPropertyLossesData(String xsemId){
        RestResponse restResponse=null;
        try{
            List<IndoorOutdoorPropertyLosses> list = enterpriseDirectEconomyService.getIndoorOutdoorPropertyLossesData(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 人员伤亡评估
     * @return
     */
    @PostMapping("/getPersonnelInjuryAssessment")
    public RestResponse getPersonnelInjuryAssessment(@RequestBody ParamsVo paramsVo){
        RestResponse restResponse=null;
        try{
            enterpriseDirectEconomyService.getPersonnelInjuryAssessment(paramsVo.getXsemId());
            restResponse = RestResponse.succeed();
            restResponse.setMessage("评估成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "评估失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 人员伤亡评估   分页查询
     * @param xsemId 当前地震事件id
     * @return
     */
    @GetMapping("/getPersonnelInjuryData")
    public RestResponse getPersonnelInjuryData(HttpServletRequest request, String xsemId){
        RestResponse restResponse=null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = enterpriseDirectEconomyService.getPersonnelInjuryData(xsemId,curPage,pageSize);
            restResponse = RestResponse.succeed(map);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 人员伤亡评估 查看死亡原因
     * @param xlrmId
     * @return
     */
    @GetMapping("/getCauseDeaths")
    public RestResponse getActionTracks(String xlrmId){
        RestResponse restResponse=null;
        try {
            List<CasualtyInvestigation> casualtyInvestigationList=enterpriseDirectEconomyService.getCauseDeaths(xlrmId);
            restResponse= RestResponse.succeed(casualtyInvestigationList);
        }catch (Exception e){
            String errormessage="查看失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 生命线系统直接经济损失评估   计算前校验
     * @param xsemId
     * @return
     */
    @GetMapping("/lifelineSystemCheckData")
    public RestResponse lifelineSystemCheckData(String xsemId){
        RestResponse restResponse=null;
        try{
            String message = enterpriseDirectEconomyService.lifelineSystemCheckData(xsemId);
            restResponse = RestResponse.succeed(message);
            restResponse.setMessage("校验成功");
        }catch (Exception e){
            String errormessage="校验失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 生命线系统直接经济损失评估
     * 通过房屋直接经济损失估算
     * @return
     */
    @PostMapping("/getLifelineSystemLoss")
    public RestResponse getLifelineSystemLoss(@RequestBody ParamsVo paramsVo){
        RestResponse restResponse=null;
        try{
            enterpriseDirectEconomyService.getLifelineSystemLoss(paramsVo);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage="计算失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 生命线系统直接经济损失评估    分页查询
     * @param xsemId   当前地震事件id
     * @return
     */
    @GetMapping("/getLifelineSystemLossData")
    public RestResponse getLifelineSystemLossData(HttpServletRequest request, String xsemId, String type){
        RestResponse restResponse=null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = enterpriseDirectEconomyService.getLifelineSystemLossData(curPage,pageSize,xsemId,type);
            restResponse = RestResponse.succeed(map);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 地震救灾投入费用评估   计算
     * @return
     */
    @PostMapping("/calculateEarthquakeReliefCostAssessment")
    public RestResponse calculateEarthquakeReliefCostAssessment(@RequestBody Map<String,String> map){
        RestResponse restResponse=null;
        try{
            List<EarthquakeReliefCostAssessment> list = enterpriseDirectEconomyService.
                    calculateEarthquakeReliefCostAssessment(map.get("xsemId"),map.get("lossRatio1"),map.get("lossRatio2"),map.get("lossRatio3"));
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询地震救灾投入费用评估
     * @return
     */
    @GetMapping("/getEarthquakeReliefCostAssessment")
    public RestResponse getEarthquakeReliefCostAssessment(String xsemId){
        RestResponse restResponse=null;
        try{
            List<EarthquakeReliefCostAssessment> list = enterpriseDirectEconomyService.getEarthquakeReliefCostAssessment(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 需安置人数评估   计算
     * @return
     */
    @PostMapping("/calculateResettlementPopulationEvaluation")
    public RestResponse calculateResettlementPopulationEvaluation(@RequestBody Map<String,String> map){
        RestResponse restResponse=null;
        try{
            List<ResettlementPopulationEvaluation> list = enterpriseDirectEconomyService.calculateResettlementPopulationEvaluation(map.get("xsemId"));
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage="计算失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询需安置人数评估
     * @return
     */
    @GetMapping("/getResettlementPopulationEvaluation")
    public RestResponse getResettlementPopulationEvaluation(String xsemId){
        RestResponse restResponse=null;
        try{
            List<ResettlementPopulationEvaluation> list = enterpriseDirectEconomyService.getResettlementPopulationEvaluation(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 地震直接经济损失汇总提示
     */
    @GetMapping("/economicLossesSummaryTips")
    public RestResponse getEconomicLossesSummaryTips(){
        RestResponse restResponse=null;
        try{
            String msg = enterpriseDirectEconomyService.economicLossesSummaryTips();
            restResponse = RestResponse.succeed();
            restResponse.setMessage(msg);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 地震直接经济损失汇总
     */
    @PostMapping("/getEconomicLossesSummary")
    public RestResponse getEconomicLossesSummary(){
        RestResponse restResponse=null;
        try{
            enterpriseDirectEconomyService.getEconomicLossesSummary();
            restResponse = RestResponse.succeed();
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查询地震直接经济损失汇总
     * @return
     */
    @GetMapping("/getEconomicLossesSummaryData")
    public RestResponse getEconomicLossesSummaryData(String xsemId){
        RestResponse restResponse=null;
        try{
            List<EconomicLossesSummary> list = enterpriseDirectEconomyService.getEconomicLossesSummaryData(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }




}
